package ca.venasse.pinventory.inventory.common;

import static com.google.common.base.CharMatcher.WHITESPACE;
import static com.google.common.base.Preconditions.checkArgument;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class PartName implements Serializable {

	private static final long serialVersionUID = 1L;

	public static PartName create(String value) {
		String newValue = WHITESPACE.trimFrom(value);
		checkArgument(!newValue.isEmpty(), "error.cannot_be_blank");
		return new PartName(newValue);
	}

	@Column(name = "part_name", length = 150, updatable = true, insertable = true)
	private String value;

	protected PartName() {
	}

	private PartName(String value) {
		this.value = value;
	}

	public String asString() {
		return this.value;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof PartName)) {
			return false;
		}
		PartName other = (PartName) obj;
		if (this.value == null) {
			if (other.value != null) {
				return false;
			}
		} else if (!this.value.equals(other.value)) {
			return false;
		}
		return true;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (this.value == null ? 0 : this.value.hashCode());
		return result;
	}

}
